package com.budwk.app.sqfwV2.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.hutool.json.JSONUtil;
import com.budwk.app.sqfw.vo.DownDataVo;
import com.budwk.app.sqfwDoc.bo.ExcelCreateConfig;
import com.budwk.app.sqfwDoc.codeGeneratorV2.generate.ResultOfExcelGenerator;
import com.budwk.app.sqfwDoc.dto.TemplateCodeUpdateDTO;
import com.budwk.app.sqfwV2.dto.dto.dayingRoad.DownExcelLedgerReqVo;
import com.budwk.app.sqfwV2.dto.dto.dayingRoad.DownFileTypesResVo;
import com.budwk.app.sqfwV2.enums.PropagandaOrScreeningEnum;
import com.budwk.app.sqfwV2.service.dulaying.PropagandaOrScreeningExcelService;
import com.budwk.app.sqfwV2.service.dulaying.PropagandaOrScreeningPdfService;
import com.budwk.starter.common.openapi.annotation.ApiDefinition;
import com.budwk.starter.common.openapi.annotation.ApiOperation;
import com.budwk.starter.common.openapi.annotation.ApiResponses;
import com.budwk.starter.common.result.Result;
import com.budwk.starter.log.annotation.SLog;
import lombok.extern.slf4j.Slf4j;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.util.NutMap;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.POST;
import org.nutz.mvc.annotation.Param;

import java.util.ArrayList;
import java.util.List;

/**
 * 都拉营街道宣传、排查记录
 * @BelongsProject: dfxy-shequ
 * @Author: yangweimin
 * @CreateTime: 2024-03-06  10:17
 * @Description: TODO
 */
@IocBean
@At("/platform/sqfw/DuLaYingPropagandaOrScreening")
@ApiDefinition(tag = "都拉营街道")
@Slf4j
public class DuLaYingPropagandaOrScreeningController {

    @Inject
    private PropagandaOrScreeningPdfService propagandaOrScreeningPdfService;
    @Inject
    private PropagandaOrScreeningExcelService propagandaOrScreeningExcelService;

    @At("/v2/submitData")
    @Ok("json")
    @POST
    @ApiOperation(name = "保存数据并创建pdf")
    @SLog(tag = "保存数据并创建pdf", value = "${args[0]}")
    public Result<?> submitData(@Param("..") TemplateCodeUpdateDTO dto) {
        propagandaOrScreeningPdfService.submitData(dto);
        return Result.success();
    }

    @At("/v2/update")
    @Ok("json")
    @POST
    @ApiOperation(name = "更新 2.0版本")
    @SaCheckLogin
    public Result<?> update(@Param("..") TemplateCodeUpdateDTO dto) {
        propagandaOrScreeningPdfService.update(dto);
        return Result.success();
    }

    @At("/v2/list")
    @Ok("json")
    @POST
    @ApiOperation(name = "获取列表数据 2.0版本")
    @ApiResponses
    @SaCheckLogin
    public Result<?> list(@Param("pageNo") Integer pageNo, @Param("pageSize") Integer pageSize,
                          @Param("sort") String sort, @Param("fillDate") String fillDate,
                          @Param("type") String type) {
        return propagandaOrScreeningPdfService.list(pageNo, pageSize, fillDate, sort, type);
    }

    @At("/v2/checkData")
    @Ok("json")
    @POST
    @ApiOperation(name = "数据检测 2.0版本")
    @SaCheckLogin
    public Result<?> checkData(@Param("..") DownExcelLedgerReqVo reqVo) {

        List<DownFileTypesResVo> downFileTypesResVos = new ArrayList<>();

        reqVo.setCheckData(true);

        // pdf
        List<ResultOfExcelGenerator> generatePdfUrlVOList = propagandaOrScreeningPdfService.downPdfList(reqVo);
        for (ResultOfExcelGenerator vo : generatePdfUrlVOList) {
            PropagandaOrScreeningEnum pdf = PropagandaOrScreeningEnum.getEnum(vo.getFileIdentity());
            DownFileTypesResVo resVo = new DownFileTypesResVo();
            resVo.setLabel(pdf.getName())
                    .setPreviewImage(List.of(pdf.getImg().split(",")))
                    .setFileCode(String.valueOf(pdf.getCode()))
                    .setCreatedAt(System.currentTimeMillis())
                    .setLocation(Integer.parseInt(pdf.getCode()))
                    .setFileName(pdf.getCompilationName())
                    .setFileType(pdf.getFileType())
                    .setHiveData(vo.getPassCreateConditionFlag() == null ? false : vo.getPassCreateConditionFlag());
            downFileTypesResVos.add(resVo);
        }

        // excel
        DownDataVo<ResultOfExcelGenerator> downDataVo = propagandaOrScreeningExcelService.createExcel(reqVo);
        List<ResultOfExcelGenerator> generateExcelUrlVOList = downDataVo.getVoList();
        for (ResultOfExcelGenerator vo : generateExcelUrlVOList) {
            DownFileTypesResVo resVo = new DownFileTypesResVo();
            PropagandaOrScreeningEnum excel = PropagandaOrScreeningEnum.getEnum(vo.getFileIdentity());
            resVo.setLabel(excel.getName())
                    .setPreviewImage(List.of(excel.getImg().split(",")))
                    .setFileCode(String.valueOf(excel.getCode()))
                    .setCreatedAt(System.currentTimeMillis())
                    .setLocation(Integer.parseInt(excel.getCode()))
                    .setFileName(excel.getCompilationName())
                    .setFileType(excel.getFileType())
                    .setHiveData(vo.getPassCreateConditionFlag() == null ? false : vo.getPassCreateConditionFlag());
            downFileTypesResVos.add(resVo);
        }
        return Result.data(NutMap.NEW().addv("list", downFileTypesResVos).addv("totalCount", downDataVo.getTotalCount()));
    }

    @At("/v2/downPdf")
    @Ok("json")
    @POST
    @ApiOperation(name = "下载pdf 2.0版本")
    @SaCheckLogin
    public Result<?> downPdf(@Param("..") DownExcelLedgerReqVo reqVo) {
        List<ResultOfExcelGenerator> generatePdfUrlVOList = propagandaOrScreeningPdfService.downPdfList(reqVo);
        return Result.data(generatePdfUrlVOList);
    }

    @At("/v2/downExcel")
    @Ok("json")
    @POST
    @ApiOperation(name = "下载excel 2.0版本")
    @SaCheckLogin
    public Result<?> downExcel(@Param("..") DownExcelLedgerReqVo reqVo) {
        DownDataVo<ResultOfExcelGenerator> downDataVo = propagandaOrScreeningExcelService.createExcel(reqVo);
        return Result.data(downDataVo.getVoList());
    }


    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "创建pdf")
    @ApiResponses
    @SLog(tag = "创建pdf", value = "${args[0]}")
    @SaCheckLogin
    public Result<?> createPdf(@Param("..") TemplateCodeUpdateDTO templateCodeUpdateDTO) {
        log.info("都拉营街道宣传、排查记录：{}", JSONUtil.toJsonStr(templateCodeUpdateDTO));
        propagandaOrScreeningPdfService.createPdfAndMsg(templateCodeUpdateDTO);
        return Result.success();
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "下载excel（数据检测、模板下载、数据下载都用此接口）")
    @SLog(tag = "下载excel", value = "${args[0]}")
    public Result<?> downExcel(@Param("excelConfigList") List<ExcelCreateConfig> excelConfigList,
                               @Param("sqfwIdList") List<String> sqfwIdList) {
        List<ResultOfExcelGenerator> excelList = propagandaOrScreeningExcelService.createExcel(excelConfigList, sqfwIdList);
        return Result.success(excelList);
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "获取单位下所有excel（数据检测、模板下载、数据下载都用此接口）")
    @SLog(tag = "获取单位下所有excel", value = "${args[0]}")
    public Result<?> createExcelToUnit(@Param("excelConfigList") List<ExcelCreateConfig> excelConfigList) {
        List<ResultOfExcelGenerator> excelList = propagandaOrScreeningExcelService.createExcelToUnit(excelConfigList);
        return Result.success(excelList);
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "pdf合集下载")
    @ApiResponses
    @SLog(tag = "pdf合集下载", value = "${args[0]}")
    @SaCheckLogin
    public Result<?> downPdfList(@Param("excelConfigList") List<ExcelCreateConfig> excelConfigList,
                                 @Param("sqfwIdList") List<String> sqfwIdList) {
        return Result.success(propagandaOrScreeningPdfService.downPdfList(excelConfigList, sqfwIdList));
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "取单位下所有pdf数据")
    @ApiResponses
    @SLog(tag = "取单位下所有pdf数据", value = "${args[0]}")
    @SaCheckLogin
    public Result<?> downPdfListToUnit(@Param("excelConfigList") List<ExcelCreateConfig> excelConfigList) {
        return Result.success(propagandaOrScreeningPdfService.downPdfListToUnit(excelConfigList));
    }
}
